package com.A21MobileSales;

import java.util.ArrayList;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.ImageView;

import com.A21MobileSales.DataBase.DatabaseHelper;

public class HomeScreen extends Activity implements OnClickListener {

	ImageButton btn_Logout;
	ImageView imgBtnSaleOrder, imgBtnCashInvoice, imgBtnDashBoard,
			imgBtnCreditInvoice, imgBtnCustomer, imgBtnProduct;
	Intent mIntent;
	Bundle mBundle;

	DatabaseHelper objDBHelper;
	ArrayList<String> AryMainMenuDetails;

	Connection cd3;
	Boolean isInternetPresent3 = false;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.home_screen);
		try {
			objDBHelper = new DatabaseHelper(HomeScreen.this);

			btn_Logout = (ImageButton) findViewById(R.id.btn_Logout);
			btn_Logout.setOnClickListener(this);

			imgBtnSaleOrder = (ImageView) findViewById(R.id.imgBtnSaleOrder);
			imgBtnSaleOrder.setOnClickListener(this);

			imgBtnCashInvoice = (ImageView) findViewById(R.id.imgBtnCashInvoice);
			imgBtnCashInvoice.setOnClickListener(this);

			imgBtnDashBoard = (ImageView) findViewById(R.id.imgBtnDashBoard);
			imgBtnDashBoard.setOnClickListener(this);

			imgBtnCreditInvoice = (ImageView) findViewById(R.id.imgBtnCreditInvoice);
			imgBtnCreditInvoice.setOnClickListener(this);

			imgBtnCustomer = (ImageView) findViewById(R.id.imgBtnCustomer);
			imgBtnCustomer.setOnClickListener(this);

			imgBtnProduct = (ImageView) findViewById(R.id.imgBtnProduct);
			imgBtnProduct.setOnClickListener(this);
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	void setViewItemMenu() {
		imgBtnSaleOrder.setEnabled(false);
		imgBtnCashInvoice.setEnabled(false);
		imgBtnDashBoard.setEnabled(false);
		imgBtnCreditInvoice.setEnabled(false);
		imgBtnCustomer.setEnabled(false);
		imgBtnProduct.setEnabled(false);

		for (int i = 0; i < AryMainMenuDetails.size(); i++) {
			if (AryMainMenuDetails.get(i).equals("Sales Order")) {
				imgBtnSaleOrder.setEnabled(true);
			} else if (AryMainMenuDetails.get(i).equals("Cash Invoice")) {
				imgBtnCashInvoice.setEnabled(true);
			} else if (AryMainMenuDetails.get(i).equals("Credit Invoice")) {
				imgBtnCreditInvoice.setEnabled(true);
			} else if (AryMainMenuDetails.get(i).equals("Customer")) {
				imgBtnCustomer.setEnabled(true);
			} else if (AryMainMenuDetails.get(i).equals("Product")) {
				imgBtnProduct.setEnabled(true);
			} else if (AryMainMenuDetails.get(i).equals("Dashboard")) {
				imgBtnDashBoard.setEnabled(true);
			}
		}
	}

	@Override
	public void onStart() {
		super.onStart();

		try {
			AryMainMenuDetails = objDBHelper.GetMainMenuDetails();
			setViewItemMenu();
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	public void Showlogoutbox(final Activity context, String title,
			String message) {

		AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
				context);

		// set title
		alertDialogBuilder.setTitle(title);
		alertDialogBuilder.setIcon(R.drawable.ic_launcher);
		// set dialog message
		alertDialogBuilder
				.setMessage(message)
				.setCancelable(false)
				.setPositiveButton("Yes",
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog, int id) {
								// if this button is clicked, close
								// current activity
								// context.finish();
								Intent _intent = new Intent();
								_intent.setClass(HomeScreen.this,
										LoginActivity.class);
								startActivity(_intent);
							}
						})
				.setNegativeButton("No", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int id) {
						// if this button is clicked, just close
						// the dialog box and do nothing
						dialog.cancel();
					}
				});

		// create alert dialog
		AlertDialog alertDialog = alertDialogBuilder.create();

		// show it
		alertDialog.show();
	}

	@Override
	public void onBackPressed() {
		Showlogoutbox(HomeScreen.this, "A21-Mobile Suite !",
				"Are you sure want to logout?");
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_Logout:
			mIntent = new Intent();
			mIntent.setClass(HomeScreen.this, LoginActivity.class);
			startActivity(mIntent);
			break;
		case R.id.imgBtnSaleOrder:
			mBundle = new Bundle();
			mBundle.putString("TrnType", "CW");
			mIntent = new Intent();
			mIntent.putExtras(mBundle);
			mIntent.setClass(HomeScreen.this, CustomerSelection.class);
			startActivity(mIntent);
			break;
		case R.id.imgBtnCashInvoice:
			mBundle = new Bundle();
			mBundle.putString("TrnType", "CA");
			mIntent = new Intent();
			mIntent.putExtras(mBundle);
			mIntent.setClass(HomeScreen.this, CustomerSelection.class);
			startActivity(mIntent);
			break;
		case R.id.imgBtnDashBoard:
			mIntent = new Intent();
			mBundle = new Bundle();
			mBundle.putString("TrnType", "");
			mIntent.setClass(HomeScreen.this, OrderSummary.class);
			mIntent.putExtras(mBundle);
			startActivity(mIntent);
			break;
		case R.id.imgBtnCreditInvoice:
			mBundle = new Bundle();
			mBundle.putString("TrnType", "CR");
			mIntent = new Intent();
			mIntent.putExtras(mBundle);
			mIntent.setClass(HomeScreen.this, CustomerSelection.class);
			startActivity(mIntent);
			break;
		case R.id.imgBtnCustomer:
			mIntent = new Intent();
			mIntent.setClass(HomeScreen.this, CustomerMaster.class);
			startActivity(mIntent);
			break;
		case R.id.imgBtnProduct:
			mIntent = new Intent();
			mIntent.setClass(HomeScreen.this, ProductMaster.class);
			startActivity(mIntent);
			break;

		default:
			break;
		}
	}

}
